package com.xuhuan.client.business.splitfile.controller;

import com.alibaba.fastjson2.JSONObject;
import com.xuhuan.client.business.splitfile.entity.FileSplitLog;
import com.xuhuan.client.business.splitfile.properties.FileSplitProperties;
import com.xuhuan.client.business.splitfile.service.IFileSplitFacadeService;
import com.xuhuan.client.business.splitfile.vo.SaveFileSplitLogVO;
import com.xuhuan.client.result.RequestResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

/**
 * @Author huan.xu
 * @Date 2023/6/17 9:24
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/client/file-split")
public class FileSplitController {

    @Autowired
    private IFileSplitFacadeService fileSplitFacadeService;

    /**
     * 获取参数配置
     *
     * @return
     */
    @PostMapping("/get-file-split-properties")
    public RequestResult getFileSplitProperties() {
        RequestResult requestResult = null;
        try {
            FileSplitProperties fileSplitProperties = fileSplitFacadeService.getFileSplitProperties();
            requestResult = RequestResult.success(fileSplitProperties);
        } catch (Exception e) {
            requestResult = RequestResult.fail(RequestResult.FAIL_CODE, e.getMessage());
            log.error("获取参数配置失败：{}", ExceptionUtils.getFullStackTrace(e));
        }
        return requestResult;
    }

    /**
     * 保存附件分片日志
     *
     * @return
     */
    @PostMapping("/save-file-split-log")
    public RequestResult saveFileSplitLog(@RequestBody SaveFileSplitLogVO saveFileSplitLogVO) {
        log.info("访问方法[{}],请求参数：{}","save-file-split-log", JSONObject.toJSONString(saveFileSplitLogVO));
        RequestResult requestResult = null;
        try {
            requestResult=fileSplitFacadeService.saveFileSplitLog(saveFileSplitLogVO);
        } catch (Exception e) {
            requestResult = RequestResult.fail(RequestResult.FAIL_CODE, e.getMessage());
            log.error("保存附件分片日志失败：{}", ExceptionUtils.getFullStackTrace(e));
        }
        return requestResult;
    }
}
